/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.danikula.videocache.util;

import ohos.app.Context;

/**
 * vp px fp转换工具
 */
public class DisplayUtils {
    /**
     * convert px to its equivalent dp
     * 将px转换为与之相等的vp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return vp值
     */
    public static int px2vp(Context context, float pxValue) {
        final float scale =
                context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * convert dp to its equivalent px
     * 将dp转换为与之相等的px
     *
     * @param context  上下文
     * @param dipValue vp值
     * @return px值
     */
    public static int vp2px(Context context, float dipValue) {
        final float scale =
                context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (dipValue * scale + 0.5f);
    }


    /**
     * convert px to its equivalent sp
     * 将px转换为fp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return fp值
     */
    public static int px2fp(Context context, float pxValue) {
        final float fontScale =
                context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (pxValue / fontScale + 0.5f);
    }


    /**
     * convert sp to its equivalent px
     * 将fp转换为px
     *
     * @param context 上下文
     * @param spValue fp值
     * @return px值
     */
    public static int fp2px(Context context, float spValue) {
        final float fontScale =
                context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (spValue * fontScale + 0.5f);
    }
}

